package com.prs.crm.action.cqunicom;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.annotation.Clew;
import com.prs.crm.context.AppContext;
import com.prs.crm.domain.cqunicom.LogSerch;
import com.prs.crm.domain.cqunicom.Movie;
import com.prs.crm.domain.hr.Person;
import com.prs.crm.exception.InValidDeleteException;
import com.prs.crm.service.cqunicom.LogSerchService;
import com.prs.crm.service.cqunicom.MovieService;
import com.prs.crm.service.hr.PersonService;
import com.prs.crm.service.sys.LogService;

/**
 * @author  WangTing
 *
 * 创建时间：2011-3-5 
 * 
 */


public class MovieAction extends BaseAction {
	private static final long serialVersionUID = 1L;

	private Movie movie;
	private MovieService  movieService;

	private Integer[] movieIds;
	private LogSerchService logSerchService;
	private Person person;
	private PersonService personService;
	
	private PaginationSupport pagination;
	private LogService logService;
	
	@Action("editMovie")
	public String edit() {
		
		return SUCCESS;
	}
	
	@Action("viewMovie")
	public String view() {
		return SUCCESS;
	}
	
	@Action("listMovie")
	public String listMovie() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Movie.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getMovieService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}
	
	@Action("selectMiove")
	public String selectMiove() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Movie.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getMovieService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}
	
	
	@Action("alterMovie")
	public String alterMovie() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Movie.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getMovieService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}
	
	@Clew(results = { "success", "none" })
	@Action(value = "saveMovie", results = {
			@Result(name = "success", type = "redirect", location = "editMovie"),
			@Result(name = "none", type = "redirect", location = "listMovie"),
			@Result(name = "error", type = "chain", location = "editMovie") })
	public String save() {
		if (movie.getId() == null) {

			LogSerch  log=new LogSerch();
			log.setCandlePeron(AppContext.getAccountContext().currentUser().getUserName());
			log.setCandleContents("添加了影片:"+movie);
			this.getLogSerchService().save(log);
			this.getMovieService().save(movie);
			 List<Person> per=this.getPersonService().getAllPersons();
		        int diacInit=0;
		        for(Person p:per){
		        	if(p.getLoginCode().equals(movie.getName())){
		        		diacInit++;
		        	}
		        }
		    if(diacInit>0){
		    	this.setPrompt("登录用户名重复");
		    	return SUCCESS;
		    }   else{ 
			Person  persons = new Person();
			persons.setLoginCode(movie.getName());
			persons.setLoginPwd(movie.getPassword());
			persons.setRole(person.getRole());
			persons.setUserStyle(movie.getUserStyle());
			persons.setUserNumber(person.getUserNumber());
//			persons.setMovie(movie);
			persons.setMarkName("否");
			persons.setUserName("电影院用户");
			this.getPersonService().save(persons);
			this.setPrompt("电影院设置成功");
			   return SUCCESS;
		    }
			
		}else{
			/* List<Person> per=this.getPersonService().getAllPersons();
		        int diacInit=0;
		        for(Person p:per){
		        	if(p.getLoginCode().equals(movie.getName())){
		        		diacInit++;
		        	}
		        }
		    if(diacInit>0){
		    	this.setPrompt("该电影用户已经存在");
		    	return SUCCESS;
		    }   else{ */
			Person persons=this.getPersonService().getPersonByMovie(movie.getId(), movie.getUserStyle());
			
//			
////			persons.setLoginCode(movie.getName());
//			persons.setLoginPwd(movie.getPassword());
//			//persons.setMovie(movie);
//			this.getPersonService().saveOrUpdate(persons);
			
			
			this.getMovieService().save(movie);
			this.setPrompt("电影院设置成功");
			   return SUCCESS;
		   // }
		}
		
		
	
		
		  
	}

	@Action(value = "removeMovie", results = {
			@Result(name = "success", type = "redirect", location = "listMovie"),
			@Result(name = "error", type = "chain", location = "listMovie") })
	public String remove() {
		try {
			if (movieIds == null) {
				this.addActionError("请选择要删除的电影院");
				return ERROR;
			}
			this.getMovieService().batchRemove(movieIds);
			return SUCCESS;
		} catch (InValidDeleteException e) {
			return ERROR;
		}
	}
	
	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	
	public PaginationSupport getPagination() {
		return pagination;
	}
	public LogService getLogService() {
		return logService;
	}
	public void setLogService(LogService logService) {
		this.logService = logService;
	}

	
	public Movie getMovie() {
		return movie;
	}

	public void setMovie(Movie movie) {
		this.movie = movie;
	}

	public MovieService getMovieService() {
		return movieService;
	}

	public void setMovieService(MovieService movieService) {
		this.movieService = movieService;
	}

	public Integer[] getMovieIds() {
		return movieIds;
	}

	public void setMovieIds(Integer[] movieIds) {
		this.movieIds = movieIds;
	}

	public static long getSerialVersionUID() {
		return serialVersionUID;
	}
	

	public Person getPerson() {
		return person;
	}

	public void setPerson(Person person) {
		this.person = person;
	}

	public PersonService getPersonService() {
		return personService;
	}

	public void setPersonService(PersonService personService) {
		this.personService = personService;
	}
	public boolean istel(String str) {
		boolean tag = false;  
		final String pattern1 = "^(([0-9]{3,4})|[0-9]{3,4}-)?[0-9]{7,8}$";
		final Pattern pattern = Pattern.compile(pattern1);
		final Matcher mat = pattern.matcher(str);
		if (!mat.find()) {
			tag = true;
		}
		return tag;
	}

	public boolean isnull(String str) {
		boolean s=true;
		if (str.trim().equals("") || str == null) {
			s= false;
		}
		return s;
	}

	public LogSerchService getLogSerchService() {
		return logSerchService;
	}

	public void setLogSerchService(LogSerchService logSerchService) {
		this.logSerchService = logSerchService;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}
	

}
